package com.handpay.zztong.hp.main.model.component;

/**
 * Created by lyang on 2017/4/10.
 */


import org.xml.sax.helpers.DefaultHandler;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import java.util.ArrayList;


public class ComponentItemHandler extends DefaultHandler {
    private ComponentItemBean mComponentItem;
    private ArrayList<ComponentItemBean> mComponentList;
    final int ITEM_CODE = 1;
    final int ITEM_TITLE = 2;
    final int ITEM_TYPE = 3;
    final int ICON_NAME = 4;
    final int UPDATE_TIME = 5;
    final int ITEM_URL = 8;

    int currentstate = 0;


    public ComponentItemHandler() {
    }


    @Override
    public void startDocument() throws SAXException {
        mComponentList = new ArrayList<ComponentItemBean>();
    }

    public ArrayList<ComponentItemBean> getComponentList() {
        return mComponentList;
    }

    @Override
    public void endDocument() throws SAXException {

    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if (localName.equals("item")) {
            mComponentItem = new ComponentItemBean();
            return;
        }
        if (localName.equals("ITEM_CODE")) {
            currentstate = ITEM_CODE;
            return;
        }
        if (localName.equals("ITEM_TITLE")) {
            currentstate = ITEM_TITLE;
            return;
        }
        if (localName.equals("ITEM_TYPE")) {
            currentstate = ITEM_TYPE;
            return;
        }
        if (localName.equals("ICON_NAME")) {
            currentstate = ICON_NAME;
            return;
        }
        if (localName.equals("UPDATE_TIME")) {
            currentstate = UPDATE_TIME;
            return;
        }
        if (localName.equals("ITEM_URL")) {
            currentstate = ITEM_URL;
            return;
        }
        currentstate = 0;
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (localName.equals("item")) {
            mComponentList.add(mComponentItem);
            return;
        }
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
        String theString = new String(ch, start, length);
        switch (currentstate) {
            case ITEM_CODE:
                mComponentItem.setItemCode(theString);
                currentstate = 0;
                break;
            case ITEM_TITLE:
                mComponentItem.setItemTitle(theString);
                currentstate = 0;
                break;
            case ITEM_TYPE:
                mComponentItem.setItemType(theString);
                currentstate = 0;
                break;
            case ICON_NAME:
                mComponentItem.setIconName(theString);
                currentstate = 0;
                break;
            case UPDATE_TIME:
                mComponentItem.setUpdateTime(theString);
                currentstate = 0;
                break;
            case ITEM_URL:
                mComponentItem.setItemUrl(theString);
                currentstate = 0;
                break;
            default:
                return;
        }
    }
}